/**
 * Copyright (c) 2018-present, Chen Guodong
 * All rights reserved.
 *
 * @flow
 */


<template>
  <section class="content">
    <Header>
      <div slot="left" @click="goBack" style="width: 100%;height: 100%;text-align: center;">
        <i class="iconfont icon-arrow-left" style="font-size: 22px"></i>
      </div>
      <div slot="middle" class="header_middle">
        <tab :line-width="3" custom-bar-width="30px">
          <tab-item selected @on-item-click="handlerHeader('NORMAL')">默认</tab-item>
          <tab-item @on-item-click="handlerHeader('HOT')">热门</tab-item>
          <tab-item @on-item-click="handlerHeader('RECOMMEND')">推荐</tab-item>
        </tab>
      </div>
    </Header>
    <div class="main">
      <!-- <mescroll-vue
        class="mescroll_content"
        ref="mescroll"
        :down="mescrollDown"
        @init="mescrollInit"
      >-->
      <div ref="mescroll" class="mescroll">
        <ul id="dataList">
          <li v-for="(item,index) in list" :key="index" @click="goDetail(item.id)">
            <div class="top">
              <div class="left">
                <img :src="item.litpic" alt>
              </div>
              <div class="right">
                <p class="title">{{item.title}}</p>
                <div class="info_box">
                  <b class="type" v-if="item.ordered">已购买</b>
                  <b class="type" v-else>{{item.price === 0 ? '免费' : `${item.price}卷`}}</b>
                  <b
                    class="status"
                    v-if="item.price!==0 && !item.ordered"
                    @click.stop="buyClass(item)"
                  >购买</b>
                  <!-- <b class="status">{{item.cataname}}</b> -->
                </div>
              </div>
            </div>
            <p class="bottom">创建时间:&nbsp;{{item.create_time}}</p>
          </li>
        </ul>
      </div>
      <!-- </mescroll-vue> -->
    </div>
  </section>
</template>

<script>
import Header from "../../components/Header/Header";
import MescrollVue from "mescroll.js/mescroll.vue";
import MeScroll from "mescroll.js";
import moment from "moment";
// import moment from 'moment';
import { Tab, TabItem } from "vux";
import { getKeChengData, reqOrderCreate } from "../../api/index.js";

export default {
  data() {
    return {
      listType: "NORMAL",
      list: [],
      mescroll: {}
      // mescrollDown: {
      //   callback: this.downCallback,
      //   auto: true,
      //   isBoth: false,
      //   textInOffset: "下拉刷新",
      //   textOutOffset: "松开立即刷新",
      //   textLoading: `<p>正在加载数据中...</p><p>最后更新: 今天${moment().format(
      //     "HH:mm"
      //   )}</p>`
      // },
      // //下拉刷新的配置. (如果下拉刷新和上拉加载处理的逻辑是一样的,则mescrollDown可不用写了)
      // mescrollUp: {
      //   page: {
      //     num: 1, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
      //     size: 10 // 每页数据的数量
      //   },
      //   auto: false,
      //   htmlNodata: '<p class="upwarp-nodata">-- 没有更多内容了 --</p>',
      //   callback: this.upCallback,
      //   empty: {
      //     // 列表第一页无任何数据时,显示的空提示布局; 需配置warpId才生效;
      //     warpId: "content", // 父布局的id;
      //     tip: "暂无相关数据~", // 提示
      //     lazyLoad: {
      //       use: false // 是否开启懒加载,默认false
      //     }
      //   }
      // }
    };
  },
  mounted() {
    // this.mescroll.triggerDownScroll();
    self = this;
    self.mescroll = new MeScroll(self.$refs.mescroll, {
      down: {
        auto: true,
        callback: self.downCallback,
        textInOffset: "下拉刷新",
        textOutOffset: "松开立即刷新",
        textLoading: `<p>正在加载数据中...</p><p>最后更新: 今天${moment().format(
          "HH:mm"
        )}</p>`
      },
      up: {
        auto: false,
        page: {
          num: 1, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
          size: 10 // 每页数据的数量
        },
        callback: self.upCallback, //上拉回调
        isBounce: false, //此处禁止ios回弹,
        htmlNodata:
          "<p style='text-align: center; color: #ccc; font-size: 12px; padding: 10px;'>-- 没有更多数据了... --</p>",
        empty: {
          //配置列表无任何数据的提示
          warpId: "dataList",
          tip:
            "<p style='text-align: center; color: #ccc; font-size: 12px; padding: 10px;'>亲,暂无相关数据哦~</p>"
        }
      }
    });
  },
  methods: {
    handlerHeader(data) {
      this.listType = data;
      this.mescroll.triggerDownScroll();
    },
    mescrollInit(mescroll) {
      this.mescroll = mescroll;
    },
    goDetail(data) {
      this.$router.push({ path: "/kechengDetail", query: { id: data } });
    },
    goBack() {
      if (window.history.length <= 1) {
        this.$router.push({ path: "/" });
        return false;
      } else {
        this.$router.go(-1);
      }
    },
    // 下拉刷新
    async downCallback(mescroll) {
      let p = JSON.parse(localStorage.getItem("selectedProject"));
      const result = await getKeChengData({
        listType: this.listType,
        sid: p.id,
        pageIndex: 1,
        pageSize: 10,
        listRows: 10
      });
      if (result.code === 1) {
        this.list = result.data.items;
        this.$nextTick(() => {
          mescroll.endSuccess(result.data.rowCount);
        });
      } else {
        this.$vux.toast.show(`获取免费列表失败:${result.msg}`);
        this.$nextTick(() => {
          mescroll.endErr();
        });
      }
    },
    // 上拉加载
    async upCallback(page, mescroll) {
      let p = JSON.parse(localStorage.getItem("selectedProject"));
      const result = await getKeChengData({
        listRows: 10,
        pageIndex: page.num,
        pageSize: page.size,
        listType: this.listType,
        sid: p.id
      });
      if (result.code === 1) {
        const arr = result.data.items || [];
        this.list = this.list.concat(arr);
        // 数据渲染成功后,隐藏下拉刷新的状态
        this.$nextTick(() => {
          mescroll.endSuccess(arr.length);
        });
      } else {
        this.$nextTick(() => {
          mescroll.endErr();
        });
      }
    },
    async buyClass(item) {
      let result = await reqOrderCreate({ relId: item.id, orderType: "CF" });
      if (result.code === 1) {
        this.$vux.toast.show(`购买成功!`);
        this.mescroll.triggerDownScroll();
      } else {
        this.$vux.toast.show(`购买课程失败:${result.msg}`);
      }
    }
  },
  components: { Header, MescrollVue, Tab, TabItem }
};
</script>

<style lang='less' scoped>
@import "../../util/theme.less";
.header_middle {
  padding: 2px 10% 0;
  position: relative;
  box-sizing: border-box;
}

.content {
  .be-full();
  .main {
    width: 100%;
    height: calc(~"100% - 46px");
    ul {
      box-sizing: border-box;
      width: 100%;
      // margin-top: 15px;
      li {
        padding: 0 10px;
        box-sizing: border-box;
        background: @white;
        margin-bottom: 15px;
        .top {
          border-bottom: 1px solid @border-base;
          .mix-flex-center();
          height: 90px;
          position: relative;
          box-sizing: border-box;
          padding: 10px 0;
          .left {
            height: 100%;
            flex: 3;

            img {
              width: 100%;
              height: 100%;
              border-radius: 5px;
              overflow: hidden;
            }
          }
          .right {
            width: 100%;
            height: 100%;
            flex: 7;
            padding-left: 10px;
            position: relative;
            .title {
            }
            .info_box {
              position: absolute;
              bottom: 0;
              width: 100%;
              .type {
                color: red;
              }
              .status {
                position: absolute;
                right: 10px;
                top: 0;
                font-size: 10px;
                border: 1px solid @red;
                color: @red;
                padding: 0 3px;
                border-radius: 3px;
              }
            }
          }
        }
        .bottom {
          line-height: 30px;
          font-size: 12px;
          text-align: left;
          color: @font-secondary;
        }
      }
    }
  }
}
</style>
